When developing software, it is often advantageous for purposes of defect analysis or optimization to instrument the software. Instrumentation of software refers to inserting additional instructions into the software so that when the software is executed, information regarding the software is recorded for subsequent review by the developer. The information varies widely based upon the purpose for instrumenting the software, but can include whether variables were initialized properly, what values they were assigned, how many times a code segment was used, recognizable patterns of code segment usage, memory usage, data access, etc.
When instrumenting software, a developer must choose a point within the compilation process to analyze the software, and also a point within the compilation process to insert the additional instructions. For example, U.S. Pat. No. 5,535,329 to Hastings discloses a method of instrumenting software after object code has been created by the compiler. Executable object code is analyzed for instrumentation points and additional instructions and data designed to instrument the software are inserted in between pre-existing instructions and data. Once the instrumentation instructions are inserted, an offset check is performed such that offsets that may be incorrect due to the new locations of the pre-existing code are corrected to insure proper execution of the software.
U.S. Pat. No. 6,314,558 to Angel at al. discloses a method of instrumenting software during the intermediate representation (IR) stage of compilation. Once source code has undergone lexical and syntax analysis, the resulting parse tree is transformed into an intermediate representation. The intermediate representation of the code is used to construct an IR tree using tree construction software. This IR tree is analyzed in order to determine instrumentation points. Instrumentation data in IR format is then injected into the IR tree to form an instrumented IR tree. The instrumented IR tree is then deconstructed to form an instrumented IR element, which is then compiled to form instrumented object code.
Methods such as those described in Hastings and Angel et al. suffer from a number of defects. First, analysis of intermediate representation or object code is less accurate in detecting possible defects, and in determining how to properly instrument code segments containing defects, because much of the context of the original source code has been lost in the transformation from source code to the intermediate representation, and even more context is lost from the intermediate representation to object code. Second, segments of source code that could be instrumented with only a few instrumentation instructions often translate into many times more instructions and memory accesses at the intermediate representation or object code level, causing additional or unneeded instrumentation instructions to be added. Lastly, during the transformation from the parse tree to the intermediate representation, modifications are made by the compiler based on control flow analysis and for purposes such as handling exceptions and destructors. Therefore, it is advantageous to analyze the code for instrumentation points prior to these modifications.